<?php
namespace app\jzshop\common\crmeb;

use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;

class ApiBase
{
    protected $baseUrl = '';
    protected $appid;
    protected $secret;
    protected $token;
    protected $expire;

    function __construct()
    {
        $jzshop = module_config('jzshop');
        $this->baseUrl = $jzshop['crmeb_url'];
        $this->appid = $jzshop['crmeb_appid'];
        $this->secret = $jzshop['crmeb_appsecret'];
        $this->token = $this->getToken();
    }
    public function getToken()
    {
        $token = cache('crmeb_token');
        if ($token) {
            $this->token = $token;
            $this->expire = time() + 7000;
            return $token;
        } else {
            $response = $this->NoAuthRequest('POST', $this->baseUrl . '/access_token', ['appid' => $this->appid, 'appsecret' => $this->secret]);
            if ($response['status'] == 200 && isset($response['data']['access_token'])) {
                $this->token = $response['data']['access_token'];
                cache('crmeb_token', $this->token);
                return $response['data']['access_token'];
            } else {

                throw new \Exception($response['msg']);
            }
        }
    }

    protected function NoAuthRequest($method = 'POST', $url, $body = [], $headers = [])
    {
        $ClientConfig = [];
        //判断$this->baseUrl是否为https
        if (strpos($this->baseUrl, 'https') !== false) {
            $ClientConfig['verify'] = env('root_path') . 'application\jzshop\common\cacert.pem';  // 忽略证书验证
            $ClientConfig['timeout'] = null; // 默认超时时间秒
        }
        $client = new Client($ClientConfig);
        $headers['Content-Type'] = 'application/json;charset=UTF-8';
        /////////////////////////////
        // $headers = [
        //     'Content-Type' => 'application/json',
        // ];
        $body = json_encode($body, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);

        // $request = new Request('POST', 'http://crmeb.lhp/outapi/access_token', $headers, $body);
        $request = new Request($method, $url, $headers, $body);

        $response = $client->sendAsync($request)->wait();
        $data = $response->getBody()->getContents();
        // dump($data);
        $data = json_decode($data, true);
        return $data;
        /////////////////////////////







        $body = json_encode($body, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
        dump([$method, $url, $headers, $body]);
        $request = new Request($method, $url, $headers, $body);
        $response = $client->sendAsync($request)->wait();
        $data = $response->getBody()->getContents();
        // dump($data);
        $data = json_decode($data, true);
        return $data;
    }

    protected function Request($method = 'POST', $url = '', $body = [], $headers = [])
    {
        $headers['Authori-zation'] = 'Bearer ' . $this->token;
        return $this->NoAuthRequest($method, $url, $body, $headers);
    }
}